<template>
  <van-icon
    :color="likeCount ? '#ffa500' : '#777'"
    :name="likeCount ? 'good-job' : 'good-job-o'"
    @click="onLike"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: "LikeArticle",
  props: {
    likeCount: {
      type: Number,
      required: true,
    },
    id: {
      type: [Number, String],
      required: true,
    },
    
  },
 methods: {
    async onLike () {
      try {
        this.$toast.loading({
          duration: 0, // 持续时间，0为永不关闭
          message: '加载中...',
          forbidClick: true // 是否禁止背景点击
        })
        if (this.likeCount) {
          await deleteLike(this.id)
        } else {
          await addLike(this.id)
        }
        this.$emit('update:like-count', this.likeCount ? 0 : 1)
      } finally {
        this.$toast.clear()
      }
    }
  }
};
</script>